/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.core.parser.antlr;

import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;

import com.funkyroach.use.core.parser.USToken;

public class USDynamicTokenStream extends CommonTokenStream implements
        TokenStream, IUSTokenStream {

    private USTokenSource tokenSource;
    USProblemReporter reporter;

    public USDynamicTokenStream(USTokenSource tokenSource) {
        super(tokenSource);
        this.tokenSource = tokenSource;
        
    }

    public void setReporter(USProblemReporter reporter) {
        this.reporter = reporter;
        tokenSource.setReporter(reporter);
    }
    
    @Override
    public Token LT(int k) {
        Token lt = super.LT(k);
        if(lt == null) return null;
        
        int startSource = reporter.getOffset(lt);
        String text = lt.getText();
        int endSource = startSource + (text != null ?text.length():1);   
        return new USToken (lt, startSource, endSource);
    }

  
}
